<?php
/**
 * 支付服务
 * @author  wyb <[<email address>]>
 */
namespace Api\Server;

class PaymentServer {

	public static $_self = null;

	private $_default_data = array( 'request_data' => '123456789' );

	/**
	 * 初始化类
	 */
	public static function init() {
		return self::$_self == null ? new self() : self::$_self;
	}

	/**
	 * 数据交互加密
	 * @param  array $[data] [<加密的数据>]
	 */
	public function aign( $data ) {
		if( !$data ) $data = $this->_default_data;
		ksort( $data );
		$aign = sha1( strtoupper( md5( http_build_query( $data ) . '&key=' . C( 'PAYMENT_KEY' ) ) ) );
		$data['aign'] = $aign;
		return $data;
	}

	/**
	 * 数据认证
	 */
	public function aignCheck( $data ) {
		unset( $data['a'] );
		unset( $data['m'] );
		unset( $data['c'] );
		$check_data = $data['aign'];
		unset( $data['aign'] );
		if( !$data ) $data = $this->_default_data;
		ksort( $data );
		$aign = sha1( strtoupper( md5( http_build_query( $data ) . '&key=' . C( 'PAYMENT_KEY' ) ) ) );
		if( $check_data == $aign ) return true;
		return false;
	}

}